package com.jdcjk.chat;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

/**
 * Created by dong on 2016/5/9.
 * 即时通讯连接类
 */
public class OpenFireConnectionManager {

    /***************************
     * 单例模式
     *************************/
    private static OpenFireConnectionManager ocm = null;

    private OpenFireConnectionManager() {
        super();
    }

    public static OpenFireConnectionManager getInstance() {
        if (ocm == null) {
            synchronized (OpenFireConnectionManager.class) {
                if (ocm == null) {
                    ocm = new OpenFireConnectionManager();
                }
            }
        }
        return ocm;

    }

    /*************************
     * 获取XMPP连接
     *********************************/
    private static AbstractXMPPConnection connection = null;

    /**
     * 获取XMPP连接
     */
    public synchronized AbstractXMPPConnection getXMPPTcpConnection() {
        if (connection == null) {
            // 设置连接配置
            XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
//            builder.setHost("192.168.1.123");
//            builder.setServiceName("192.168.1.123");
            builder.setHost("123.57.43.25");
            builder.setServiceName("123.57.43.25");
            builder.setPort(5222);
//			builder.setSendPresence(false);
            // 允许登陆成功后更新在线状态
            builder.setSendPresence(true);
            builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            // 获取XMPP连接实例
            connection = new XMPPTCPConnection(builder.build());
        }
        return connection;
    }

    /**
     * 建立连接
     **/
    public synchronized void connect() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 建立连接
                    connection.connect();
                    if (listener != null) {
                        listener.onConnected();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 断开连接
     */
    public void disconnect() {
        try {
            if (connection != null && connection.isConnected()) {
                // 断开连接
                connection.disconnect();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放连接
     */
    public void release() {
        try {
            if (connection != null) {
                System.out.println("*************** 释放连接 ******************");
                // 断开连接
                disconnect();
                connection = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // OF监听器
    private OnOpenFireConnectListener listener = null;

    // 设置监听器
    public void setCallbackListener(OnOpenFireConnectListener listener) {
        this.listener = listener;
    }

    public interface OnOpenFireConnectListener {
        // 连接成功回调方法s
        public void onConnected();
    }
}